<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/31
 * Time: 11:04
 */

namespace p5\objects;


class CollectionUtils
{
    public static function pick($collection, $propertyName, $removeDuplicate = false)
    {
        $result = [];
        foreach ($collection as $bean) {
            $property = PropertyUtils::getProperty($bean, $propertyName);
            if (!empty($property)) {
                if ($removeDuplicate) {
                    if (in_array($property, $result)) {
                        continue;
                    }
                }
                array_push($result, $property);
            }
        }
        return $result;
    }

    public static function find($collection, $properties)
    {
        $result = self::findAll($collection, $properties);
        if (empty($result)) {
            return null;
        }
        return $result[0];
    }

    public static function findAll($collection, $properties)
    {
        $result = [];
        foreach ($collection as $bean) {
            $match = true;
            foreach ($properties as $key => $value) {
                $property = PropertyUtils::getProperty($bean, $key);
                if ($property . "" !== $value . "") {
                    $match = false;
                    break;
                }
            }
            if ($match) {
                array_push($result, $bean);
            }
        }
        return $result;
    }

    public static function groupBy($list, $key)
    {
        $hash = [];
        foreach ($list as $line) {
            $value = PropertyUtils::getProperty($line, $key);
            if (!array_key_exists($value, $hash)) {
                $hash[$value] = [];
            }
            array_push($hash[$value], $line);
        }
        return $hash;
    }

    public static function groupCount($list, $key)
    {
        $temp = self::groupBy($list, $key);
        $hash = [];
        foreach ($temp as $key => $value) {
            $hash[$key] = count($value);
        }
        return $hash;
    }


    public static function toHash($list, $keyPropertyName = "", $valuePropertyName = "")
    {
        $map = [];
        if (empty($keyPropertyName)) {
            if (count($list) > 0) {
                $keys = array_keys(JsonUtils::toArray($list[0]));
                $keyPropertyName = $keys[0];
                $valuePropertyName = $keys[1];
            }
        }
        foreach ($list as $line) {
            $key = PropertyUtils::getProperty($line, $keyPropertyName);
            $value = PropertyUtils::getProperty($line, $valuePropertyName);
            $map[$key] = $value;
        }
        return $map;
    }

    public static function toMap($menus, $property)
    {
        $map = [];
        foreach ($menus as $menu) {
            $map[PropertyUtils::getProperty($menu, $property)] = $menu;
        }
        return $map;
    }


    public static function sum($list, $column)
    {
        $total = 0;
        foreach ($list as $bean) {
            $total = $total + PropertyUtils::getProperty($bean, $column, 0) * 1;
        }
        return $total;
    }

    public static function min($list, $column)
    {
        $current = 9999999999;
        foreach ($list as $bean) {
            $temp = PropertyUtils::getProperty($bean, $column, 0) * 1;
            $current = $temp > $current ? $current : $temp;
        }
        return $current;
    }

    public static function max($list, $column)
    {
        $current = 0;
        foreach ($list as $bean) {
            $temp = PropertyUtils::getProperty($bean, $column, 0) * 1;
            $current = $temp < $current ? $current : $temp;
        }
        return $current;
    }

}